export const leftFillNum = (num, targetLength) => {
  return num.toString().padStart(targetLength, 0);
};
// minutes
export const addMinutes = (date, minutes) => {
  return new Date(date.getTime() + minutes * 60000);
};
export const subMinutes = (date, minutes) => {
  return new Date(date.getTime() - minutes * 60000);
};

// hours
export const addHours = (date, hours) => {
  return new Date(date.getTime() + hours * 3600000);
};
export const subHours = (date, hours) => {
  return new Date(date.getTime() - hours * 3600000);
};

// years
export const addYears = (date, years) => {
  let nd = new Date(date);
  nd.setFullYear(date.getFullYear() + years);
  return nd;
};
export const subYears = (date, years) => {
  let nd = new Date(date);
  nd.setFullYear(date.getFullYear() - years);
  return nd;
};

function formatMs(date){
  return date.getMilliseconds().toString().padStart(3,'0')
}
// to string
export const formatTime = (date) => {
  return `${leftFillNum(date.getHours(), 2)}:${leftFillNum(
    date.getMinutes(),
    2
  ).padStart(2, '0')}:${leftFillNum(date.getSeconds(), 2).padStart(2, '0')}`;
};

export const formatDate = (date) => {
  return `${date.getFullYear()}-${leftFillNum(date.getMonth() + 1).padStart(
    2,
    '0'
  )}-${leftFillNum(date.getDate()).padStart(2, '0')}`;
};

export const formatDateBySlash = (date) => {
  return `${date.getFullYear()}/${leftFillNum(date.getMonth() + 1).padStart(
    2,
    '0'
  )}/${leftFillNum(date.getDate()).padStart(2, '0')}`;
};

export const formatDateTime = (date) => {
  return formatDate(date) + ' ' + formatTime(date);
};

export const formatDateTimems = (date) => {
  return formatDate(date) + ' ' + formatTime(date)+'.'+formatMs(date);
};

export const formatDateTimeString = (datetime_str) => {
  if(datetime_str==null){
    return ''
  }
  return formatDateTime(new Date(datetime_str));
};

export const formatTimeString = (datetime_str)=>{
   if(datetime_str==null){
    return ''
  }
  return formatTime(new Date(datetime_str))
}

export function formatMyDate(val){
  if (val&&val.includes('T')){
    const arr = val.split('T')
    return `${arr[0].replace(/-/g,'.')} ${arr[1].slice(0,8)}`
  }
  return val
}

export function formatMyTime(val){
  if (val=='') return val
  let totalseconds = parseInt(val)
  let hour = 3600
  // let daysout = Math.floor(totalseconds / day);
  let hoursout = (totalseconds/hour);
  return hoursout.toFixed(2)

}

